﻿using UnityEngine;
using System.Collections;

public enum PlayingMovieType
{
	LogoPicture,
	Opening,
	Title,
	End_StartUI
}

public class Video : MonoBehaviour 
{
	public MovieTexture _MovieTexture_Opening;
	public MovieTexture _MovieTexture_Title;
	public SpriteRenderer _LogoSprite;
	public UIWidget _TitleUI;

	private AudioSource _AudioSrc;

	public PlayingMovieType MovieType;

	void Awake()
	{
		_AudioSrc = this.GetComponent<AudioSource> ();

		_TitleUI.transform.localScale = new Vector3 (0, 0, 0);
	}

	void Start()
	{
		_MovieTexture_Opening.loop = false;
		_MovieTexture_Opening.Stop ();
		_AudioSrc.Stop();
		
		_MovieTexture_Title.loop = false;
		_MovieTexture_Title.Stop ();

	}

	void Update()
	{
		if (MovieType == PlayingMovieType.LogoPicture) 
		{
			if (Input.anyKeyDown) 
			{
				SwitchToNextState();
			}
		} 
		else
		{
			if(Input.GetKeyDown(KeyCode.Escape))
			{
				SwitchToNextState();
			}
		}
	}

	void OnGUI()
	{
		switch(MovieType)
		{
		case PlayingMovieType.Opening:
		{
			GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),_MovieTexture_Opening,ScaleMode.StretchToFill);
			if(!_MovieTexture_Opening.isPlaying)
			{
				SwitchToNextState();
			}
		}break;
		case PlayingMovieType.Title:
		{
			GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height),_MovieTexture_Title,ScaleMode.StretchToFill); 
			if(!_MovieTexture_Title.isPlaying)
			{
				SwitchToNextState();
			}
		}break;
		case PlayingMovieType.End_StartUI:
		{

		}break;
		}


	}

	void SwitchToNextState()
	{
		switch(MovieType)
		{
		case PlayingMovieType.LogoPicture:
		{
			MovieType = PlayingMovieType.Opening;
			_LogoSprite.enabled = false;
			_MovieTexture_Opening.Stop();
			_MovieTexture_Opening.Play ();
			_AudioSrc.Play();
//			_MovieTexture_Opening.Stop();
//			MovieType = PlayingMovieType.Title;
//			_MovieTexture_Title.Stop();
//			_MovieTexture_Title.Play();
		}break;
		case PlayingMovieType.Opening:
		{
			_MovieTexture_Opening.Stop();
			MovieType = PlayingMovieType.Title;
			_MovieTexture_Title.Stop();
			_MovieTexture_Title.Play();
		}break;
		case PlayingMovieType.Title:
		{
			MovieType = PlayingMovieType.End_StartUI;
			_MovieTexture_Title.Stop();
			Application.LoadLevel("Concept");
			_TitleUI.transform.localScale = new Vector3 (1.0f, 1.0f, 1.0f);
		}break;
		}
	}
}
